<template>
  <el-row class="row">
    <el-col :span="24">
      <DialogEdit :visible.sync="visible" :datas="businessData" @refresh="refresh"></DialogEdit>
      <Portlet title="商家">
        <el-form ref="searchForm" class="el-form-col search-form"
                 :model="search">
          <el-row :gutter="15">
            <el-col :lg="4" :md="6">
              <el-form-item label="" prop="name">
                <el-input v-model="search.name" placeholder="商家名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="prin">
                <el-input v-model="search.prin" placeholder="商家联系人"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="phone">
                <el-input v-model="search.phone" placeholder="商家联系电话"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="8">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span>
                    </el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="userName">
                <el-input v-model="search.userName" placeholder="账号姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="" prop="state">
                <el-select v-model="search.state" clearable placeholder="状态">
                  <el-option
                    label="启用"
                    value="1">
                  </el-option>
                  <el-option
                    label="禁用"
                    value="0">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span>
                    </el-button>
                  </el-col>
                  <!--<el-col :span="8">-->
                  <!--<el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>-->
                  <!--</el-col>-->
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </Portlet>
    </el-col>

    <el-col :span="24">
      <basic-table
        ref="dataTable"
        url="/ms-warehouse-order/basicData/findCargoOwnerList"
        :columns="columns"
      >
        <template slot="column" >
          <el-table-column  fixed="right" align="center" width="100px" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="changeState(scope.row)"
              >{{scope.row.state == 1 ? '停用': '启用'}}
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="show(scope.row)"
              >
                修改
              </el-button>
            </template>
          </el-table-column>
        </template>
        <el-button slot="buttons" type="primary" @click.prevent="show()">新增</el-button>
      </basic-table>
    </el-col>
  </el-row>
</template>
<script>
import {copy, dotData, alert} from '@/utils'
import {getInstallBusinessType, getInstallServiceType} from '@/utils/selectOptions'
import moment from 'moment'
import {unixTimestampFilter} from '@/filters'
import DialogEdit from '@/views/oms/basicData/owner/DialogEdit'
export default {
  name: 'basicDataOwnerIndex',
  components: {
    DialogEdit
  },
  filters: {
    unixTimestampFilter
  },
  data() {
    return {
      businessData: {},
      search: {
        code: '',
        name: '',
        state: '',
        prin: '',
        userName: '',
        phone: ''
      },
      businessArr: [],
      serviceArr: [],
      isOpen: false,
      searchLoading: false,
      costLoading: false,
      visible: false,
      rowId: '',
      columns: [
        {
          label: '商家编号',
          data: 'code'
        },
        {
          label: '商家名称',
          data: 'name'
        },
        {label: '账号姓名', data: 'userName'},
        {label: '绑定登录手机号', data: 'userPhone'},
        {label: '角色', data: 'roleName'},
        {label: '商家联系人', data: 'prin'},
        {label: '商家联系电话', data: 'phone'},
        {
          label: '添加时间',
          data: 'foundTime',
          width: '160',
          render: (data, full) => {
            return unixTimestampFilter(data, 'YYYY-MM-DD HH:mm:ss')
          }
        },
        {label: '操作人', data: 'founderName'},
        {
          label: '状态',
          data: 'state',
          render(data, full) {
            if (data === 1) {
              return '启用'
            } else if (data === 0) {
              return '停用'
            }
            return ''
          }
        }
      ]
    }
  },
  created() {
    getInstallBusinessType().then(arr => {
      this.businessArr = arr
    })
    getInstallServiceType().then(arr => {
      this.serviceArr = arr
    })
  },
  methods: {
    disabledDate(time) {
      return time.getTime() < moment(this.search.distributionStartDate).format('x')
    },
    searchFunc(search) {
      let data = copy(search, true)
      this.searchLoading = true
      this.$refs.dataTable.search(data).finally(_ => {
        this.searchLoading = false
      })
    },
    resetFields(searchForm) {
      this.$refs.searchForm.resetFields()
    },
    refresh() {
      this.searchFunc(this.search)
    },
    create() {
      this.$router.push({path: '/ss-oms/basic-data/owner/create'})
    },
    show(data) {
      if (!data) {
        data = {}
      }
      this.businessData = data
      this.visible = true
    },
    changeState(data) {
      this.costLoading = true
      let title = data.state === 1 ? '你确定停用该商家吗?<br>停用后,录单界面中将不可再选择该商家进行录单。<br>对于该商家已录订单不会造成影向。' : '你确定启用该商家吗?<br>启用后,录单界面中可选择该商家进行录单。'
      this.$confirm(title, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true,
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(() => {
        this.$ajax.post('/ms-warehouse-order/basicData/modifyCargoState', {cargoOwnerId: data.id, state: data.state === 1 ? 0 : 1}).then(response => {
          const success = dotData(response, 'success')
          if (success) {
            alert('操作成功！', '', 'success')
            this.refresh()
          }
          this.costLoading = false
        })
      }).catch(() => {
        this.costLoading = false
      })
    }
  }
}
</script>
